<?php

namespace app\api\controller;

use app\admin\model\Storemessage as StoremessageMdl;

use think\Db;
use think\Exception;

class Storemessage extends Base
{

    /**
     * 店铺消息
     */
    public function getList(){
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;
        if (empty($param->storeId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where =  "`type` = 0 OR (`type` = 1 AND `storeId` = '" . $param->storeId . "')";

        $model = new StoremessageMdl();
        $fields = 'id, title, content, createDate';
        $result = $model->where($where)->field($fields)->limit($start, $length)->select();
        $total = $model->where($where)->count();

        if (empty($result)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到店铺消息');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 详情
     */
    public function getDetail(){
        $param = self::getHttpParam();

        if (empty($param->id)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'storeId 不能为空');
        }

        $where = [
            'id' => $param->id,
        ];

        $fields = 'title, content, createDate';
        $item = StoremessageMdl::where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.error_code'), self::NOT_DATA, '未查到店铺消息');
        }

        return show(config('status.success_code'), self::MSG_SUCCESS, $item);
    }

}